<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace oss_cs\driver;

/**
 * Description of AliOssApi
 *
 * @author DevPHP
 * @version 2018-4-13
 */
class AliyunOSS {

    protected $ossClient;

    public function __construct() {
        try {
            $this->ossClient = new \OSS\OssClient(
                config('sdkaccount.ali_oss_access_key_id'),
                config('sdkaccount.ali_oss_access_key_secret'),
                config('sdkaccount.ali_oss_endpoint'), false);
        } catch (\OSS\Core\OssException $e) {
            printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
            printf($e->getMessage() . "\n");
            throw $e;
        }
    }

    /**
     * 
     * @param type $bucket
     * @param type $object
     * @param type $file
     * @return array array('server'=>'AliyunOSS','date'=>'Fri, 13 Apr 2018 10:21:05 GMT','content-length'=>'0','connection'=>'keep-alive','x-oss-request-id'=>'5AD08491AC946530D9A1203C','etag'=>'"BB58A573107BBF5ADAFA4B9C577DEAA0"','x-oss-hash-crc64ecma'=>'4025651135532981234','content-md5'=>'u1ilcxB7v1ra+kucV33qoA==','x-oss-server-time'=>'111','info'=>array('url'=>'http://chaoyueoss.oss-cn-beijing.aliyuncs.com/public/aabbcc.txt','content_type'=>NULL,'http_code'=>200,'header_size'=>335,'request_size'=>405,'filetime'=>-1,'ssl_verify_result'=>0,'redirect_count'=>0,'total_time'=>0.188,'namelookup_time'=>0,'connect_time'=>0.047,'pretransfer_time'=>0.047,'size_upload'=>1822,'size_download'=>0,'speed_download'=>0,'speed_upload'=>9691,'download_content_length'=>0,'upload_content_length'=>1822,'starttransfer_time'=>0.078,'redirect_time'=>0,'redirect_url'=>'','primary_ip'=>'59.110.185.172','certinfo'=>array(),'primary_port'=>80,'local_ip'=>'192.168.7.227','local_port'=>57526,'method'=>'PUT',),'oss-request-url'=>'http://chaoyueoss.oss-cn-beijing.aliyuncs.com/public/aabbcc.txt','oss-redirects'=>0,'oss-stringtosign'=>'PUT text/plain Fri, 13 Apr 2018 10:21:04 GMT /chaoyueoss/public/aabbcc.txt','oss-requestheaders'=>array('Accept-Encoding'=>'','Content-Type'=>'text/plain','Date'=>'Fri, 13 Apr 2018 10:21:04 GMT','Host'=>'chaoyueoss.oss-cn-beijing.aliyuncs.com','Authorization'=>'OSS LTAIt8HBl0SMrNOR:jsNx+zbgOmxZG9szMey4769opPc=',),'body'=>false,)
     * @version 2018-4-13
     */
    public function uploadFile($bucket, $object, $file) {
        return $this->ossClient->uploadFile($bucket, $object, $file);
    }

    public function uploadDir($bucket, $prefix, $localDirectory) {
        $this->ossClient->uploadDir($bucket, $prefix, $localDirectory);
    }

    public function listBuckets() {
        return $this->ossClient->listBuckets();
    }

    public function createBucket($bucket) {
        $this->ossClient->createBucket($bucket, \OSS\OssClient::OSS_ACL_TYPE_PUBLIC_READ);
    }

}
